package action.zpcj;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import services.zpcj.PicUploadServices;
import com.util.Component;
import com.Const;
import com.PublicSystem;
/**
* 照片批量上传
* @author bjh
* @version Fri May 06 17:59:17 CST 2016 gjx
* @since 1.0
*/
public class PicUploadAction extends HttpServlet {	
	PublicSystem sys = PublicSystem.getInstance();
	PicUploadServices ss = new PicUploadServices();
	private static final long serialVersionUID = 1L;
	private String CONTENT_TYPE = "text/html; charset=UTF-8";
	private String code="UTF-8";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	/**
	 * 处理表单post提交
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			
			 response.setContentType(CONTENT_TYPE);
			 request.setCharacterEncoding(code);
			boolean flag = false;
			DiskFileItemFactory factory = new DiskFileItemFactory();
			HashMap<String, String> map = new HashMap<String, String>();
			// 设置内存缓冲区，超过后写入临时文件
			factory.setSizeThreshold(10240000);
			// 设置临时文件存储位置
		
			String eventid = Component.getQuest("eventid", request);	
			
			String fileurlpath = Const.server_path + "/plupload/"+eventid;												
			File file = new File(fileurlpath);
			if (!file.exists())file.mkdirs();
			factory.setRepository(file);
			ServletFileUpload upload = new ServletFileUpload(factory);
			// 设置单个文件的最大上传值
			upload.setFileSizeMax(102400000l);
			// 设置整个request的最大值
			upload.setSizeMax(102400000l);
			upload.setHeaderEncoding("UTF-8");
			List list = new ArrayList();
			List<?> items = upload.parseRequest(request);
			FileItem item = null;
			String fileName = null;			
			for (int i = 0 ; i < items.size(); i++){
				item = (FileItem) items.get(i);
				fileName = item.getName();
				// 保存文件
				if (!item.isFormField() && item.getName().length() > 0) {
					String ffType = fileName.substring(fileName.lastIndexOf(".") + 1);
					for(int j = 0; j < Const.dataType.length; j++) {
						if(Const.dataType[j].trim().equals(ffType)) {
							flag = true;
						}
					}
					String acc001="";
					String aac003="";
					if(fileName.length() >= 12){
						acc001 = fileName.substring(0, 10);
						aac003 = fileName.substring(10, fileName.lastIndexOf("."));
					} else {
						acc001 = fileName.substring(0,  fileName.lastIndexOf("."));
					}
					if(true == flag) {
						item.write(new File(fileurlpath + "/" + fileName));
						list.add(acc001 + "," + aac003);				
					} else {
						throw new IOException("IP地址" + request.getRemoteAddr() + "上传的附件后缀名为" + ffType + "的类型不符合要求");  
					}
				}
			}
			eventid = ss.insertsql(list, "AZ02", eventid);
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}